<script setup lang="ts">
defineOptions({
  name: 'IndexView',
})
import { Swiper, SwiperSlide } from 'swiper/vue'
import banner from '@/layout/banner.vue'
import 'swiper/css'
import { getProductList } from '@/api/product'
import { getNewsList } from '@/api/news'
import { getPolicyList } from '@/api/policy'
import type { Product } from '@/types/product'
import { usePaging } from '@/hooks/pagination'
import { useDict, transformDict } from '@/hooks/dict'
import { useRouter } from 'vue-router'
import { getStatisall } from '@/api/common'
useDict('productMainGuaranteeType')
const router = useRouter()
const { guarantee_type: guaranteeType } = useDict('guarantee_type')
const { queryParams, setQueryParms } = usePaging()
setQueryParms({
  mainGuaranteeWay: '1',
})
const productIndex = ref(0)
const productList = ref<Product[]>([])

watch(productIndex, (i) => {
  console.log(guaranteeType.value)

  queryParams.mainGuaranteeWay = guaranteeType.value[i]?.value
  loadProduct()
})

onMounted(() => {
  loadProduct()
  loadNews()
  loadInstitution()
  loadCaseList()
  getStatisallFuntion()
})

const statisall = ref({})
function getStatisallFuntion() {
  getStatisall().then((res) => {
    console.log(res)
    statisall.value = res.data
  })
}

function handleDetail(id: string) {
  router.push({ path: `/financial-products/${id}` })
}
function loadProduct() {
  getProductList(queryParams).then((res) => {
    productList.value = res.rows.slice(0, 6)
  })
}

const formatAmount = (value: number) => {

  if (!value) {
    return '0'
  }
  return (value / 10000).toFixed(2)
}


const currentType = ref('news')
watch(currentType, (str) => {
  if (str === 'news') {
    loadNews()
  }

  if (str === 'policy') {
    loadPolicy()
  }
})

// 新闻资讯等信息

import type { News } from '@/types/news'
import { useDetail } from '@/hooks/detail'
const { navDetail } = useDetail()
const newsList = ref<News[]>([])
const newsTopsList = ref<News[]>([])
function loadNews() {
  getNewsList({ pageNum: 1, pageSize: 20 }).then((res) => {
    newsList.value = res.rows
    newsTopsList.value = res.rows.filter((f) => f.isTop == 1).slice(0, 4)
  })
}

function loadPolicy() {
  getPolicyList({ pageNum: 1, pageSize: 20 }).then((res) => {
    newsList.value = res.rows
  })
}

// 金融机构
import { getInstitution } from '@/api/institution'
import type { Institution } from '@/types/institution'
const institutionList = ref<Institution[]>([])
const swiperInstitutionList = ref<Institution[][]>([])

function loadInstitution() {
  getInstitution({ pagesNum: 1, pageSize: 100 }).then((res) => {
    institutionList.value = res.rows
    const interval = res.rows.length / 10

    for (let i = 0; i < interval; i++) {
      swiperInstitutionList.value.push(res.rows.slice(i * 10, (i + 1) * 10))
    }
  })
}

// 案例
import { getCaseList } from '@/api/news'
const caseList = ref<{ name: string; company: string; story: string; coverImg: string }[]>([])
function loadCaseList() {
  getCaseList().then((res) => {
    caseList.value = res.rows
  })
}
function switchSwiper(index) {
  if (swiperInstance.value) {
    curretnIndex.value = index
    swiperInstance.value.slideTo(index)
  }
}
const swiperInstance = ref(null)
const curretnIndex = ref(0)

// 保存 Swiper 实例
const onSwiper = (swiper) => {
  swiperInstance.value = swiper
}

const navToUrl = () => {
  window.location.href = 'https://credit.hd.gov.cn:8081/#/government-policy'
}

const onSlideChange = (e) => {
  console.log(e)

  curretnIndex.value = e.activeIndex
}

import { useImage } from '@/utils/Img'
const { baseImg } = useImage()
</script>
<template>
  <div class="bg-white">
    <banner />
    <div class="w-1200 m-auto">
      <div class="information">
        <div class="information-item">
          <div class="information-item-tp"><span>{{ statisall?.financingNeedsNum || 0 }}</span><span
              class="text">笔</span>
          </div>
          <div class="information-item-bt">
            <span>累计融资需求笔数</span>
          </div>
        </div>
        <div class="information-item">
          <div class="information-item-tp"><span>{{ formatAmount(statisall?.financingNeedsAmount || 0) }}</span><span
              class="text">亿+</span></div>
          <div class="information-item-bt">
            <span>累计融资需求金额</span>
          </div>
        </div>
        <div class="information-item">
          <div class="information-item-tp"><span>{{ statisall?.creditLoanNum || 0 }}</span><span class="text">笔</span>
          </div>
          <div class="information-item-bt">
            <span>累计授信笔数</span>
          </div>
        </div>
        <div class="information-item">
          <div class="information-item-tp"><span>{{ formatAmount(statisall?.creditAmount || 0) }}</span><span
              class="text">亿+</span>
          </div>
          <div class="information-item-bt">
            <span>累计授信金额</span>
          </div>
        </div>
        <div class="information-item">
          <div class="information-item-tp"><span>{{ statisall?.loanNum || 0 }}</span><span class="text">笔</span></div>
          <div class="information-item-bt">
            <span>累计放款笔数</span>
          </div>
        </div>
        <div class="information-item">
          <div class="information-item-tp"><span>{{ formatAmount(statisall?.loanAmount || 0) }}</span><span class="text">亿+</span>
          </div>
          <div class="information-item-bt">
            <span>累计放款金额</span>
          </div>
        </div>
      </div>
      <!-- 3723笔
累计融资需求笔数
597.55亿+
累计融资需求金额
69211笔
累计授信笔数
1525.74亿+
累计授信金额
68699笔
累计放款笔数
781.71亿+
累计放款金额 -->
      <div>
        <div class="special" @click="navToUrl"></div>
      </div>

      <div class="product">
        <div class="tl"><span>金融产品</span></div>
        <div class="content">
          <div style="
              width: 995px;
              height: 45px;
              margin: 15px auto 0px;
              display: flex;
              justify-content: space-around;
            ">
            <div v-for="(item, i) in guaranteeType" :key="i" @click="productIndex = i"
              :class="{ productTypeQ: i == productIndex }" class="productType">
              <span class="">{{ item.label }}贷</span>
            </div>
            <!-- <div class="productType"><span class="">抵押类</span></div>
            <div class="productType"><span class="">质押类</span></div>
            <div class="productType"><span class="">保证类</span></div> -->
          </div>

          <div class="bank mt-5">
            <ul class="flex gap-5">
              <li v-for="(item, i) in productList" :key="i" @click="handleDetail(item.id)" class="item">
                <div class="tp flex items-center justify-around">
                  <div class="logo">
                    <img :src="item.orgLogo" />
                  </div>
                  <div class="type" style="
                      width: 120px;
                      white-space: nowrap;
                      overflow: hidden;
                      text-overflow: ellipsis;
                    ">
                    {{ item.productName }}
                  </div>
                  <div class="btn mr-5">
                    {{ transformDict('productMainGuaranteeType', item.mainGuaranteeWay) }}
                  </div>
                </div>
                <div class="bt flex items-center justify-around">
                  <div class="rate ft bt-item flex-1">
                    <p class="rateNum text-center" style="margin-left: 0px">
                      {{ item.interestRateLimitLower }}-{{ item.interestRateLimitUpper }}
                      <span class="text-3.5">%</span>
                    </p>
                    <p class="text" style="margin-left: 0px">利率范围</p>
                  </div>
                  <div class="ft bt-item flex-1">
                    <p class="rateNum" style="margin-left: 0px">
                      {{ item.amountLimitLower }}-{{ item.amountLimitUpper }}万
                    </p>
                    <p class="text" style="margin-left: 0px">贷款额度</p>
                  </div>
                  <div class="ft bt-item flex-1">
                    <p class="rateNum" style="margin-left: 0px">
                      {{ item.loanPeriodLimitLower }}-{{ item.loanPeriodLimitUpper }}月
                    </p>
                    <p class="text" style="margin-left: 0px">贷款期限</p>
                  </div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="etp" @click="openEnterprise">
      <h1>我是中小企业</h1>
      <div class="btn">我要融资</div>
    </div>
    <div class="app">
      <div class="content w-1200 m-auto">
        <div class="content-title">最新动态</div>
        <div style="width: 420px; margin: 0px auto; height: 44px">
          <div @click="currentType = 'news'" class="orgType" :class="{ orgTypeQ: currentType === 'news' }">
            <span>新闻资讯</span>
          </div>
          <div @click="currentType = 'policy'" class="orgType" :class="{ orgTypeQ: currentType === 'policy' }">
            <span>政策法规</span>
          </div>
        </div>
        <div class="mt-5.2">
          <div v-show="currentType === 'news'" class="recommend flex gap-4">
            <div class="recommend-item" v-for="(item, i) in newsTopsList" @click="navDetail(item.id, '1')" :key="i">
              <img :src="item.coverImage" alt="" class="recommend-item-bg" style="object-fit: cover" />
              <div class="recommend-item-cover">
                <div class="recommend-item-cover-title">
                  {{ item.title }}
                </div>
                <div class="recommend-item-cover-time">
                  {{ item.createTime }}
                </div>
              </div>
            </div>
          </div>
          <div class="list flex flex-wrap justify-between">
            <div class="list-item" v-for="(item, i) in newsList" :key="i"
              @click="navDetail(item.id, currentType === 'news' ? '1' : '2')">
              <div class="list-item-title">{{ item.title }}</div>
              <div class="list-item-time">
                {{ item.createTime }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="institution">
      <div class="tl"><span>入驻金融机构</span></div>
      <div class="content w-1200 m-auto">
        <swiper :slides-per-view="1" :space-between="50">
          <swiper-slide v-for="(list, i) in swiperInstitutionList" :key="i">
            <div class="xinyong_img">
              <img v-for="(item, i2) in list" :key="i2" :src="baseImg(item.logo)" alt="" />
            </div>
          </swiper-slide>
        </swiper>
      </div>
    </div>

    <div class="fincialStory" style="margin-top: 60px">
      <div class="content w-1200 m-auto">
        <div class="content-title">融资故事</div>
        <swiper @swiper="onSwiper" autoplay @slideChange="onSlideChange" :slides-per-view="1" :space-between="50">
          <swiper-slide v-for="(item, i) in caseList" :key="i" style="padding-bottom: 70px">
            <div class="swiper-item flex w-full relative">
              <div class="swiper-item-bg"></div>
              <div class="swiper-item-business-card">
                <img :src="baseImg(item.historyUrl)" alt="" class="swiper-item-business-card-bg" />
                <div class="swiper-item-business-card-container">
                  <img :src="item.actorUrl" alt="" class="actorImage w-30 h-30" />
                  <div class="swiper-item-business-card-container-name">
                    {{ item.name }}
                  </div>
                  <div class="swiper-item-business-card-container-company">
                    {{ item.company }}
                  </div>
                </div>
              </div>
              <div class="swiper-item-description">
                {{ item.story }}
              </div>
            </div>
          </swiper-slide>
        </swiper>
        <div id="gallery-thumbs"
          class="swiper-container gallery-thumbs flex justify-center swiper-container-horizontal">
          <swiper :slides-per-view="4" :space-between="10">
            <swiper-slide v-for="(item, i) in caseList.slice(0, 4)" :key="i" class="m-4">
              <div class="w-16 h-16 swiperImg swiper-item border border-solid border-x-black"
                :class="{ noFilter: curretnIndex === i }" @click="switchSwiper(i)">
                <img class="w-16 h-16 imgHover" :src="baseImg(item.historyUrl)" alt="" />
              </div>
            </swiper-slide>
          </swiper>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.swiperImg {
  filter: blur(1px);
}

::v-deep .swiper {
  // overflow: visible;
}

.noFilter {
  filter: blur(0);
}

.swiperImg:hover {
  transform: scale(1.1);
  transition: all 0.5s;
}

.fincialStory {
  padding-bottom: 40px;

  .content {
    .gallery-thumbs {
      // width: 350px;
      // height: 90px;

      img {
        width: 100%;
        height: auto;
      }

      .swiper-slide {
        width: 70px !important;
        height: 65px;
        margin-top: 20px;
      }
    }

    .swiper-container {
      margin-left: auto;
      margin-right: auto;
      position: relative;
      // overflow: hidden;
      z-index: 1;
    }

    .swiper-item-bg {
      position: absolute;
      z-index: -1;
      width: 1180px;
      height: 280px;
      left: 20px;
      top: 20px;
      background-color: #fff;
      border: 1px solid rgba(0, 81, 255, 0.2);
      background-image:
        url(@/assets/images/douhao.png),
        url();
      background-repeat: no-repeat, no-repeat;
      background-size:
        87px 75px,
        87px 75px;
      background-position:
        310px 30px,
        1040px 180px;
    }

    .swiper-item-business-card-bg {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      width: 100%;
      height: 100%;
      -o-object-fit: cover;
      object-fit: cover;
    }

    .swiper-item-business-card-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-items: center;
      margin: 0 auto;
      z-index: 1;
    }

    .swiper-item-business-card-container-name {
      font-size: 24px;
      color: #fff;
      text-align: center;
      margin-top: 24px;
    }

    .swiper-item-business-card-container-company {
      font-size: 18px;
      color: #fff;
      text-align: center;
      margin-top: 10px;
    }

    .swiper-item-description {
      margin-left: 163px;
      margin-top: 90px;
      width: 565px;
      height: 130px;
      color: #575c67;
      font-size: 16px;
      line-height: 28px;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      justify-items: center;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
    }

    .swiper-item-business-card {
      position: relative;
      width: 300px;
      height: 280px;
      display: flex;
      align-items: center;
      justify-items: center;
    }
  }
}

.institution {
  .tl {
    width: 1200px;
    text-align: center;
    margin: 0 auto;
    height: 90px;
    line-height: 90px;
    font-size: 28px;
    font-family:
      PingFangSC-Medium,
      PingFang SC;
    font-weight: 500;
    color: #252b3a;
  }

  .xinyong_img {
    height: 214px;

    img {
      width: 186px;
      height: 55px;
      display: block;
      float: left;
      background: #fff;
      border: 1px solid rgba(0, 81, 255, 0.2);
      margin: 20px 26px;
      -o-object-fit: contain;
      object-fit: contain;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;
      padding: 5px 10px;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
    }
  }
}

.app {
  padding-bottom: 20px;
  width: 100%;
  background-color: #f9fbff;
}

.content {
  .content-title {
    padding: 40px 0 30px;
    font-size: 28px;
    color: #252b3a;
    font-family:
      PingFangSC-Medium,
      PingFang SC;
    font-weight: 500;
    text-align: center;
  }

  .list {
    .list-item {
      width: calc(50% - 41px);
      padding: 19px 0;
      cursor: pointer;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-pack: justify;
      -ms-flex-pack: justify;
      justify-content: space-between;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      border-bottom: 1px dashed #d4dcf0;
      margin-right: 82px;

      &:nth-of-type(2n) {
        margin-right: 0;
      }

      .list-item-title {
        width: 440px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        color: #252b3a;
        font-size: 16px;
      }

      .list-item-time {
        color: #252b3a;
        font-size: 16px;
      }
    }
  }

  .recommend {
    .recommend-item {
      position: relative;
      width: calc(25% - 12px);
      height: 197px;
    }

    .recommend-item-cover {
      position: absolute;
      bottom: 0;
      height: 98px;
      width: 100%;
      background-image: -webkit-gradient(linear,
          left top,
          left bottom,
          from(hsla(0, 0%, 93.3%, 0)),
          color-stop(rgba(29, 83, 160, 0.4)),
          to(#1d53a0));
      background-image: linear-gradient(hsla(0, 0%, 93.3%, 0), rgba(29, 83, 160, 0.4), #1d53a0);

      .recommend-item-cover-title {
        font-size: 16px;
        color: #fff;
        margin: 45px 20px 0;
        text-align: left;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .recommend-item-cover-time {
        margin-top: 8px;
        font-size: 12px;
        color: #eee;
        margin-left: 20px;
        margin-right: 20px;
        text-align: right;
      }
    }
  }

  .orgType {
    width: 175px;
    height: 44px;
    background: #0051ff;
    -webkit-transform: skewX(-30deg);
    transform: skewX(-30deg);
    text-align: center;
    line-height: 44px;
    float: left;
    margin-right: 10px;
    cursor: pointer;

    span {
      font-family:
        PingFangSC-Light,
        PingFang SC;
      color: #fff;
      -webkit-transform: skewX(30deg);
      transform: skewX(30deg);
      display: inline-block;
      font-size: 18px;
      font-weight: 300;
    }
  }

  .orgTypeQ {
    width: 199px;
    height: 50px;
    background: #ff8f2d;
    text-align: center;
    line-height: 50px;
    float: left;
    margin-right: 14px;
    margin-left: 17px;
    cursor: pointer;
    margin-top: -7px;

    span {
      font-size: 22px;
      font-weight: 600;
    }
  }
}

.etp {
  height: 260px;
  background-image: url(@/assets/images/etpbg.png);
  margin-top: 20px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  text-align: center;

  h1 {
    font-size: 40px;
    font-family:
      PingFangSC-Regular,
      PingFang SC;
    font-weight: 400;
    color: #fff;
    margin-top: 25px;
    height: 56px;
    display: inline-block;
  }

  .btn {
    width: 280px;
    height: 60px;
    margin: 10px auto 0;
    font-size: 34px;
    font-family:
      PingFangSC-Medium,
      PingFang SC;
    font-weight: 500;
    color: #fff;
    text-align: center;
    line-height: 60px;
    cursor: pointer;
    background: -webkit-gradient(linear,
        left top,
        left bottom,
        from(#fde586),
        color-stop(49%, #ee8441),
        to(#ffac6b));
    background: linear-gradient(180deg, #fde586, #ee8441 49%, #ffac6b);
    -webkit-box-shadow: 0 0 10px 0 #0c3077;
    box-shadow: 0 0 10px 0 #0c3077;
    border-radius: 44px;
  }
}

.product {
  width: 100%;
  height: 530px;
  background: #fff;

  .bank {
    ul {
      margin: 0;
      padding: 0;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -ms-flex-wrap: wrap;
      flex-wrap: wrap;
      -webkit-box-orient: horizontal;
      -webkit-box-direction: normal;
      -ms-flex-direction: row;
      flex-direction: row;
      -webkit-box-pack: start;
      -ms-flex-pack: start;
      justify-content: flex-start;

      li {
        width: 380px;
        height: 153px;
        border: 1px solid rgba(0, 81, 255, 0.2);
        text-align: center;

        .bt {
          .text {
            font-size: 14px;
            font-family:
              PingFangSC-Regular,
              PingFang SC;
            font-weight: 400;
            color: #666;
            margin-left: 42px;
            margin-top: 5px;
          }

          .rateNum {
            font-size: 20px;
            font-family:
              PingFangSC-Medium,
              PingFang SC;
            font-weight: 500;
            color: #3e8eff;
            margin-left: 42px;
            height: 28px;
            line-height: 28px;
          }
        }

        .tp {
          height: 76px;
          padding: 10px;
          box-sizing: border-box;

          .btn {
            width: 44px;
            height: 18px;
            border: 1px solid #c2c0c0;
            text-align: center;
            font-size: 12px;
            font-family:
              PingFangSC-Regular,
              PingFang SC;
            font-weight: 400;
            color: #b8b8b8;
            line-height: 18px;
          }

          .type {
            font-size: 18px;
            font-family:
              PingFangSC-Regular,
              PingFang SC;
            font-weight: 400;
            color: #252b3a;
            text-align: center;
          }

          .logo {
            width: 150px;
            height: 40px;
            overflow: hidden;

            img {
              width: auto;
              height: 40px;
            }
          }
        }
      }
    }
  }

  .productTypeQ {
    background: #ff8f2d !important;
    font-size: 22px !important;
    font-family:
      PingFangSC-Semibold,
      PingFang SC;
    font-weight: 600;
    text-align: center;
    float: left;
    cursor: pointer;
  }

  .productType {
    width: 175px;
    height: 44px;
    background: #0051ff;
    text-align: center;
    line-height: 44px;
    float: left;
    margin-right: 10px;
    cursor: pointer;
    font-size: 18px;
    font-weight: 300;
    font-family:
      PingFangSC-Light,
      PingFang SC;

    span {
      font-family:
        PingFangSC-Light,
        PingFang SC;
      color: #fff;
      display: inline-block;
    }
  }

  .tl {
    width: 1200px;
    margin: 0 auto;
    height: 90px;
    font-size: 28px;
    font-family:
      PingFangSC-Medium,
      PingFang SC;
    font-weight: 500;
    color: #252b3a;
    text-align: center;
    line-height: 98px;
  }

  .content {
    width: 100%;
    margin: 0 auto;
    height: 440px;
  }
}

.special {
  margin: 20px auto 0;
  height: 100px;
  background-image: url(@/assets/images/special.png);
  cursor: pointer;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  text-align: center;
}

.information {
  width: 1200px;
  margin: 40px auto 0;
  height: 160px;
  background: #fff;
  border: 1px solid rgba(0, 81, 255, 0.1);
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;

  .information-item {
    width: 200px;
    text-align: center;
    margin-top: 34px;

    span {
      font-size: 40px;
      font-family: Bebas;
      font-weight: 700;
      color: #ff8f2d;
    }

    .text {
      color: #999;
      font-size: 16px;
      margin-right: 5px;
    }

    .information-item-bt {
      span {
        font-size: 16px;
        font-family:
          PingFangSC-Medium,
          PingFang SC;
        font-weight: 500;
        color: #333;
        line-height: 30px;
      }
    }
  }
}
</style>
